﻿<script lang="ts" name="devicetype" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useDevicetypeApi } from '/@/api/equipment/devicetype';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const devicetypeApi = useDevicetypeApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  typeNumber: [{required: true, message: '请选择设备类型编码！', trigger: 'blur',},],
  parentId: [{required: true, message: '请选择父级Id！', trigger: 'change',},],
  deviceTypeName: [{required: true, message: '请选择设备类型名称！', trigger: 'blur',},],
  deviceTypeEnabled: [{required: true, message: '请选择是否启用！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
  const data = await devicetypeApi.getDropdownData(false).then(res => res.data.result) ?? {};
  state.dropdownData.parentId = data.parentId ?? [];
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await devicetypeApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await devicetypeApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="devicetype-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="设备类型编码" prop="typeNumber">
							<el-input v-model="state.ruleForm.typeNumber" placeholder="请输入设备类型编码" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="父级Id" prop="parentId">
							<el-cascader
								:options="state.dropdownData.parentId"
								:props="{ checkStrictly: true, emitPath: false }"
								v-model="state.ruleForm.parentId"
								placeholder="请选择父级Id"
								clearable
								filterable
								class="w100">
								<template #default="{ node, data }">
									<span>{{ data.label }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="设备类型名称" prop="deviceTypeName">
							<el-input v-model="state.ruleForm.deviceTypeName" placeholder="请输入设备类型名称" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="是否启用" prop="deviceTypeEnabled">
							<el-switch v-model="state.ruleForm.deviceTypeEnabled" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="备注" prop="deviceTypeNotes">
							<el-input v-model="state.ruleForm.deviceTypeNotes" placeholder="请输入备注" maxlength="32" show-word-limit clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>